<template>
  <div class="ann-add-wrapper">
    <el-form class="ann-add-form"
             :rules="rules"
             :model="announcement"
             ref="announcementForm">
      <el-form-item class="ann-add-form-title-wrapper" prop="title">
        <span class="ann-add-title-span">公告标题 :</span>
        <el-input class="ann-add-title-input"
                  type="text"
                  placeholder="请输入公告标题"
                  v-model="announcement.title"
                  maxlength="24"
                  show-word-limit></el-input>
      </el-form-item>
      <div class="ann-add-form-content-wrapper">
        <el-form-item class="ann-add-form-content" prop="content">
          <span class="ann-add-form-content-span">公告内容 :</span>
          <el-input class="ann-add-form-content-input"
                    type="text"
                    rows="10"
                    placeholder="请输入公告内容"
                    v-model="announcement.content"
                    maxlength="180"
                    show-word-limit></el-input>
        </el-form-item>
        <el-form-item class="ann-add-form-operations">
          <el-button type="primary" @click="addAnnouncement">发布公告</el-button>
          <el-button type="danger" @click="resetContent">重置</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: "AnnAdd",
    data() {
      return {
        announcement: {
          title: '',
          content: '',
          departmentId: null,
          majorId: null,
          classesId: null,
          teacherId: null,
          publisherName: ''
        },
        rules: {
          title: [{ required: true, message: '请输入公告标题', trigger: 'blur' }],
          content: [{ required: true, message: '请输入公告内容', trigger: 'blur' }]
        }
      }
    },
    methods: {
      addAnnouncement() {
        this.$refs['announcementForm'].validate((valid) => {
          if (valid) {
            this.$confirm('是否确定发布该公告?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.getRequest('/admin/info').then(resp => {
                if (resp) {
                  this.announcement.publisherName = resp.name
                  this.postRequest('/annmanagement/annadd/', this.announcement).then(resp => {
                    if (resp) {
                      this.announcement = {
                        title: '',
                        content: '',
                        departmentId: null,
                        majorId: null,
                        classesId: null,
                        teacherId: null,
                        publisherName: ''
                      }
                    }
                  })
                }
              })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消删除'
              })
            })
          }
        })

      },
      resetContent() {
        this.announcement.content = ''
      }
    }
  }
</script>

<style lang="less">

  .ann-add-wrapper {

    .ann-add-form {
      width: 90%;
      margin-top: 40px;
      margin-left: 40px;

      .ann-add-form-title-wrapper {
        box-shadow: 0 2px 4px rgba(0, 0, 0, .20), 0 0 6px rgba(0, 0, 0, .08);
        border-radius: 4px;
        padding: 20px 30px;

        .ann-add-title-span {
          width: 70px;
        }

        .ann-add-title-input input.el-input__inner {
          border-color: transparent;
        }
      }

      .ann-add-form-content-wrapper {
        box-shadow: 0 2px 4px rgba(0, 0, 0, .20), 0 0 6px rgba(0, 0, 0, .08);
        border-radius: 4px;
        padding: 20px 30px;
        height: 300px;

        .ann-add-form-content {
          height: 80%;

          .ann-add-form-content-span {
            width: 80px;
            margin-bottom: 190px;
          }

          .ann-add-form-content-input input.el-input__inner {
            padding-bottom: 215px;
            padding-top: 25px;
            border-color: transparent;

            &::placeholder {

            }
          }
        }

        .ann-add-form-operations {
          display: flex;
          justify-content: center;
        }
      }

    }
  }
</style>